package cn.lyh.edu.controller;

import cn.lyh.annotation.EnableSysLog;
import cn.lyh.base.BaseResult;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduBanner;
import cn.lyh.edu.service.IEduBannerService;
import cn.lyh.edu.service.dto.BannerQueryCriteria;
import cn.lyh.utils.PageVo;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("banner")
public class EduBannerController {
private final IEduBannerService eduBannerService;

    public EduBannerController(IEduBannerService eduBannerService) {
        this.eduBannerService = eduBannerService;
    }
//    @EnableSysLog("获取轮播图片列表数据")
    @GetMapping
    public ResponseEntity<Object> getList(BannerQueryCriteria queryCriteria,
                                          PageVo pageVo){
        Pageable pageable = PageRequest.of(pageVo.getPageIndex()-1, pageVo.getPageSize(),
                Sort.Direction.DESC,"id");
        return new ResponseEntity<>(eduBannerService.getList(queryCriteria,pageable), HttpStatus.OK);
    }

//    @EnableSysLog("新增轮播图片信息")
    @PostMapping
    public BaseResult addBanner(@RequestBody EduBanner eduBanner){
        eduBannerService.addBanner(eduBanner);
        return BaseResult.success("添加成功");
    }

//    @EnableSysLog("根据ID获取轮播图片的详情信息")
    @GetMapping("/{id}")
    public BaseResult detail(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("获取信息失败");
        }
        return BaseResult.success(eduBannerService.getById(id));
    }

//    @EnableSysLog("更新轮播图片信息")
    @PutMapping
    public BaseResult editBanner(@RequestBody EduBanner eduBanner){
        eduBannerService.editBanner(eduBanner);
        return BaseResult.success("更新成功");
    }

//    @EnableSysLog("删除轮播图片信息")
    @DeleteMapping("/{id}")
    public BaseResult delete(@PathVariable Long id){
        if(null==id){
            throw new BadRequestException("删除信息失败");
        }
        eduBannerService.deleteById(id);
        return BaseResult.success("删除成功");
    }


    
}
